package com.bdqn.crm.repository;


import com.bdqn.crm.entity.User;
import com.bdqn.crm.pojo.UserInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    List<User> findByUsrName(String usrName);

    List<User> findByUsrNameLike(String usrName);


    List<User> findByUsrNameLikeAndUsrRoleIdAndUsrFlag(String usrName, Long usrRoleId, Integer usrFlag);


    @Query("select u from User u where u.usrRoleId=?1")
    List<User> findByRoleIdHQL(Long roleId);


    @Query(value = "select u.* from sys_user u where u.usr_role_id=:roleId", nativeQuery = true)
    List<User> findByRoleIdSQL(@Param("roleId") Long roleId);


    @Transactional(timeout = 10)
    @Modifying
    @Query("update User u set u.usrName=?1 where u.usrId=?2")
    int modifyNameById(String usrName, Long usrId);


    List<User> findUsersByName(String usrName);


    @Query("select u from User u where u.usrRoleId=?1")
    Page<User> findPageByUsrRoleId(Long roleId, Pageable pageable);


    @Query("select u.usrId as usrId, u.usrName as usrName, u.usrPassword as usrPassword, u.usrRoleId as usrRoleId, u.usrFlag as usrFlag, r.roleName as roleName from User u, Role r where u.usrRoleId=r.roleId and u.usrId=?1")
    UserInfo getUserInfo(Long usrId);

}
